<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace User\Controller;
use Common\Lib\Think\Page;
use Common\Controller\AdminBaseController;

class AdminOauthController extends AdminBaseController
{
    const CNAME = '第三方用户';

    protected $oauthUser_model;

    public function _initialize() {
        parent::_initialize();
        $this->oauthUser_model = D("Common/OauthUser");
        $this->assign('TITLE', self::CNAME);
    }

    public function index() {
        $where = array();
        // 查询满足要求的总记录数
        $count = $this->oauthUser_model->where($where)->count();
        // 实例化分页类 传入总记录数和每页显示的记录数
        $page = new Page($count, 16);
        // 分页显示输出
        $show = $page->show();
        $list = $this->oauthUser_model
            ->where($where)
            ->order("create_time DESC")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count);
        $this->display(); // 输出模板
    }


    /**
     * 删除前台用户
     */
    public function delete() {
        $oauth_id = I('post.oauth_id', 0, 'intval');

        if (is_numeric($oauth_id)) {
            $this->oauthUser_model->delete($oauth_id);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /**
     * 启用或禁用第三方用户
     */
    public function changeStatus() {

        $oauth_id = I('post.oauth_id',0,'intval');

        if (!empty($oauth_id)) {

            if (!$detail = $this->oauthUser_model->find($oauth_id)) {
                $this->error('请选择要改变状态的'.self::CNAME);
            }
            $data['oauth_id'] = $oauth_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->oauthUser_model->save($data) !== false) {
                $this->success(self::CNAME.'.状态改变成功', U('index'));
            } else {
                $this->error(self::CNAME.'状态改变失败');
            }

        } else {
            $this->error('请选择要改变状态的'.self::CNAME);
        }
    }

}